package coder.nav.sdk;

import coder.nav.utils.HttpUtils;
import com.alibaba.fastjson2.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 聚合-节假日
 * @author Bosen 2024/10/23 11:31
 */
public class JuheCalendarAPI {

    private static final String API = "http://apis.juhe.cn/fapig/calendar/day";

    private static final String KEY = "e8f8d9a175fac5824afe097c2155b676";

    private static String FORMAT_DATE = "yyyy-MM-dd";

    private static boolean IS_WORK = false;

    public static JSONObject getResult(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, String> params = new HashMap<>();
        params.put("key", JuheCalendarAPI.KEY);
        params.put("date", sdf.format(date));
        String response = HttpUtils.sendGetRequest(JuheCalendarAPI.API, params, null);
        return JSONObject.parse(response);
    }

    public static boolean isWork(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = sdf.format(date);
        if(formatDate.equals(JuheCalendarAPI.FORMAT_DATE)) {
            return JuheCalendarAPI.IS_WORK;
        }
        JuheCalendarAPI.FORMAT_DATE = formatDate;
        JSONObject json = getResult(date);
        String statusDesc = json.getJSONObject("result").getString("statusDesc");
        JuheCalendarAPI.IS_WORK = "工作日".equals(statusDesc);
        return JuheCalendarAPI.IS_WORK;
    }
    
}
